﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;

namespace Clarius.Silverlight.Streaming.Library
{
    internal static class WebUtil
    {
        internal static String HTTP_METHOD_DELETE = "DELETE";
        internal static String HTTP_METHOD_GET = "GET";
        internal static String HTTP_METHOD_PUT = "PUT";
        internal static String HTTP_METHOD_POST = "POST";

        internal static HttpWebRequest CreateAndSetupWebRequest(Account account, String urlParams)
        {
            String url;

            if (String.IsNullOrEmpty(urlParams))
            {
                url = String.Format("{0}/{1}", account.ServiceRoot, account.Id);
            }
            {
                url = String.Format("{0}/{1}{2}", account.ServiceRoot, account.Id, urlParams);
            }
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.AllowAutoRedirect = true;
            request.Credentials = new NetworkCredential(account.Id, account.Key);
            return request;
        }

        internal static HttpWebRequest GetWebRequest(Account account)
        {
            return CreateAndSetupWebRequest(account, String.Empty);
        }

        internal static HttpWebRequest GetWebRequest(Account account, String urlParams)
        {
            return CreateAndSetupWebRequest(account, urlParams);
        }

        internal static String GetResponseText(HttpWebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream);
            return streamReader.ReadToEnd();
        }

        internal static String GetResponseErrorText(HttpWebResponse response)
        {
            String responseText = GetResponseText(response);

            // TODO: replace this with a regex
            responseText = responseText.Replace("<error>", "");
            responseText = responseText.Replace("</error>", "");
            return responseText;
        }

        internal static void RethrowIfNotWebException(Exception ex)
        {
            if (!(ex is WebException))
            {
                throw ex;
            }
        }

        internal static HttpStatusCode GetStatusCode(WebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            HttpWebResponse webResponse = response as HttpWebResponse;
            if (webResponse == null)
            {
                throw new ArgumentException("response is not a HttpWebResponse type", "response");
            }

            return webResponse.StatusCode;
        }

        internal static void ThrowUnknownError(Exception ex)
        {
            throw new SilverlightStreamingException("Unknown error", ex);
        }
    }
}
